"""
Case Type   : MOT
Case Name   : 结合MOT表，游标相关DDL
Create At   : 2020/10/10
Owner       : opentestcase001
Description :
    1.设置enable_incremental_checkpoint参数为off,并重启数据库
    2.创建普通MOT行存表 2.创建存储过程，定义游标
    3.恢复环境默认配置，修改enable_incremental_checkpoint为on，并重启数据库
Expect      :
    1.设置成功，重启数据库成功
    2.创建内存表成功，存储过程创建成功，游标定义成功，清理环境成功
    3.修改参数成功，重启数据库成功
History     :
    modified by opentestcase042,2023.01.05，优化MOT用例
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Mot_datatype_test(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.commonsh = CommonSH
        self.common = Common()
        self.config = 'enable_incremental_checkpoint'
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('------------修改配置，并重启数据库------------')
        show_msg = self.common.show_param(self.config)
        if 'off' not in show_msg:
            msg1 = self.primary_sh.execute_gsguc('set',
                                                 self.constant.GSGUC_SUCCESS_MSG,
                                                 f'{self.config}=off')
            self.logger.info(msg1)
            msg2 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg2)
            msg3 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg3)
        show_msg = self.common.show_param(self.config)
        self.assertTrue('off' in show_msg)

    def test_MOT_CURSOR_DDL(self):
        text = '-----------------step:创建MOT表，定义游标，清理数据;expect:成功------------------'
        self.logger.info(text)
        self.t_name = 't_mot_0058'
        self.pro_name = 'pro_mot_0058'
        self.sql_cmd1 = f'''
                        drop foreign table if exists {self.t_name};
                        create foreign table {self.t_name}(id int,name varchar(10));
                        '''
        self.sql_cmd2 = f'''
                        drop procedure if exists {self.pro_name};
                        create or replace procedure {self.pro_name}()
                        as
                        declare
                            type cursor_type is ref cursor;
                            C1 cursor_type;
                            sql_str varchar(100);
                            C2 varchar;
                        begin
                            for i in 0..10 loop
                                insert into {self.t_name} values(i,i||'a');
                            end loop;
                            sql_str := 'select name from {self.t_name} where id=6;';
                            execute immediate sql_str;
                            open C1 for sql_str;
                            fetch C1 into C2;
                            raise info 'output:%',C2;
                            close C1;
                        end;
                        '''
        self.sql_cmd3 = f'''
                        call {self.pro_name}();
                        drop procedure {self.pro_name};
                        drop foreign table {self.t_name};
                        '''

        msg4 = self.primary_sh.execut_db_sql(self.sql_cmd1)
        msg5 = self.primary_sh.execut_db_sql(self.sql_cmd2)
        msg6 = self.primary_sh.execut_db_sql(self.sql_cmd3)
        self.logger.info(msg4)
        self.logger.info(msg5)
        self.logger.info(msg6)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg4)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, msg5)
        self.assertIn(self.constant.DROP_PROCEDURE_SUCCESS_MSG, msg6)

    def tearDown(self):
        self.logger.info('------------this is tearDown-------------')
        self.logger.info('-----------恢复配置，enable_incremental_checkpoint=on-----------')
        show_msg = self.common.show_param(self.config)
        if 'on' not in show_msg:
            msg7 = self.primary_sh.execute_gsguc('set',
                                                 self.constant.GSGUC_SUCCESS_MSG,
                                                 f'{self.config}=on')
            self.logger.info(msg7)
            msg8 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg8)
            msg9 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg9)
        show_msg = self.common.show_param(self.config)
        self.assertTrue('on' in show_msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
